<?php
// @formatter:off
/**
 * @file WCParserBehaviorVAR.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-08-05 00:26:28 +0000 (Mon, 05 Aug 2013) $
 *
 * $Id: WCParserBehaviorVAR.php 100 2013-08-05 00:26:28Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/branches/wcomix-1.0/includes/parser/WCParserBehaviorVAR.php $
 */
// @formatter:on

//
// Loading father class.
require_once dirname(__FILE__)."/WCParserBehavior.php";

//
// Adding the function to evaluate by this behavior.
$wcParserBehaviorMatrix["VAR"] = "WCParserBehaviorVAR";

/**
 * @class WCParserBehaviorVAR This is a specific behavior that returns a variables
 * value. When it's used it looks like:
 * @code
 * <:VAR:/>name<::/>
 * @endcode
 */
class WCParserBehaviorVAR extends WCParserBehavior {
	/**
	 * This method perform a check of the object, sets the current status
	 * (variable $this->_ok) and return it.
	 *
	 * @return boolean Returns the current status after a check.
	 */
	public function check() {
		//
		// A VAR behavior must belong to a parser node.
		if($this->_node) {
			//
			// Loading node's children.
			$children = $this->_node->children();
			//
			// A VAR behavior requires at least one child belonging to
			// its parser node.
			if(!isset($children[0])) {
				//
				// Its parser node has no children. Error message
				// is set and the internal status becomes false.
				$this->_errorMsg = "node has more than one children";
				$this->_ok = false;
			} elseif(!is_string($children[0])) {
				//
				// The first child of node is not a string and it
				// cannot be considered as a name. Error message
				// is set and the internal status becomes false.
				$this->_errorMsg = "node's first child is not a name";
				$this->_ok = false;
			} else {
				//
				// At this point, everything checks out.
				$this->_ok = true;
				$this->_errorMsg = "Ok";
			}
		} else {
			//
			// Behavior's node is not present. Error message is set
			// and the internal status becomes false.
			$this->_errorMsg = "VAR node is not set";
			$this->_ok = false;
		}
		//
		// Returning current object status.
		return $this->isOk();
	}
	/**
	 * This method actually performs the function represented based on its
	 * node's data.
	 *
	 * @return string Returns the value of a variable using node's contents as
	 * a name. Otherwise, returns false.
	 */
	public function get() {
		//
		// Setting the default value to be returned.
		$out = false;
		//
		// VAR behavior will only be evaluated when this behavior has no
		// issues.
		if($this->isOk()) {
			//
			// Creating a shortcut to the parser.
			$parser = $this->_node->parser();
			//
			// Loading node's children.
			$children = $this->_node->children();
			//
			// Getting the first child as a variable name.
			$variable = "{$children[0]}";
			//
			// Asking the parser for this variable.
			$out = $parser->$variable;
		}
		//
		// Returning evaluation results.
		return $out;
	}
}
?>